package com.magina.machine.listener;

import com.magina.machine.model.ApproveEvent;
import com.magina.machine.model.ApproveStatus;
import com.magina.machine.model.DemoClass;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.statemachine.StateContext;
import org.springframework.statemachine.StateContext.Stage;
import org.springframework.statemachine.StateMachine;
import org.springframework.statemachine.listener.StateMachineListenerAdapter;
import org.springframework.statemachine.transition.Transition;
import org.springframework.stereotype.Component;

@Slf4j
@Component("approveStateListener")
public class ApproveStateListener extends StateMachineListenerAdapter<ApproveStatus, ApproveEvent> {

    @Autowired
    @Qualifier("reRedisson")
    private RedissonClient reRedisson;

    @Override
    public void stateContext(StateContext<ApproveStatus, ApproveEvent> stateContext) {
        if (stateContext.getStage().equals(Stage.STATE_ENTRY)) {
            DemoClass demoClass = (DemoClass) stateContext.getMessageHeader("demoClass");
            ApproveStatus sourceId = stateContext.getSource().getId();
            ApproveStatus targetId = stateContext.getTarget().getId();
//            if(null != targetId){
//                demoClass.setStatus(targetId.name());
//            }
                //比如记录日志
                log.info("==>save change log is {},sourceid: {},targeId: {}",demoClass,sourceId,targetId );
//            }

        }
    }

    //目前发现这个方法只能获取状态.不能处理数据
    @Override
    public void transition(Transition<ApproveStatus, ApproveEvent> transition) {
        ApproveStatus sourceId = transition.getSource().getId();
        ApproveStatus targetId = transition.getTarget().getId();
        if(transition.getTarget().getId() == ApproveStatus.DRAFT) {
            log.info("==>approval data build. status is draft!");
            return;
        }
        if(sourceId == ApproveStatus.DRAFT && targetId == ApproveStatus.NON_APPROVAL) {
            log.info("==>approval data {}. status is {}!",ApproveEvent.LAUNCH,ApproveStatus.NON_APPROVAL);
            return;
        }

        if(sourceId == ApproveStatus.NON_APPROVAL && targetId == ApproveStatus.ON_APPROVAL) {
            log.info("==>approval data {}. status is {}!",ApproveEvent.APPROVE,ApproveStatus.ON_APPROVAL);
            return;
        }
        if(sourceId == ApproveStatus.ON_APPROVAL && targetId == ApproveStatus.COMPLETE_APPROVAL) {
            log.info("==>approval data {}. status is {}!",ApproveEvent.COMPLETE,ApproveStatus.COMPLETE_APPROVAL);
            return;
        }
        if(sourceId == ApproveStatus.ON_APPROVAL && targetId == ApproveStatus.REFUSE_APPROVAL) {
            log.info("==>approval data refuse. status is refused!");
            return;
        }
    }

    @Override
    public void stateMachineError(StateMachine<ApproveStatus, ApproveEvent> stateMachine, Exception exception) {
        super.stateMachineError(stateMachine, exception);
    }

    /**
     * 时间间隔分钟数，向上取整
     *
     * @param startTime
     * @param endTime
     * @return
     */
    private static int getDurationInMinute(long startTime, long endTime) {
        return (int) Math.ceil((endTime - startTime) / 1000 / 60.0);
    }
}